﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace CloudUpload
{
    static class Program
    {
        static public string ARG_ENDPOINT = "endpoint";
        static public string ARG_CONTAINER = "container";
        static public string ARG_SIGNATURE = "signature";
        static public string ARG_SOURCEFILE = "sourcefile";
        static public string ARG_TARGETBLOB = "target";

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Environment.ExitCode = 0;
            Arguments appargs = new Arguments(Environment.GetCommandLineArgs());

            if (appargs.Parameters.Count > 1)
            {
                string endpoint = appargs[ARG_ENDPOINT];
                string container = appargs[ARG_CONTAINER];
                string signature = appargs[ARG_SIGNATURE];
                string sourcefile = appargs[ARG_SOURCEFILE];
                string target = appargs[ARG_TARGETBLOB];

                if (System.IO.File.Exists(sourcefile))
                {
                    if (Helper.UnitUploadFile(endpoint, container, signature, sourcefile, target))
                    {
                        Environment.ExitCode = 0;
                    }
                    else
                    {
                        Environment.ExitCode = 1;
                    }
                }
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Main());
            }
        }
    }

    /// <summary>
    /// Arguments class
    /// </summary>
    public class Arguments
    {
        // Variables
        public Dictionary<string, string> Parameters;

        // Constructor
        public Arguments(string[] Args)
        {
            Parameters = new Dictionary<string, string>();
            Regex Spliter = new Regex(@"^-{1,2}|^/|=|:", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            Regex Remover = new Regex(@"^['""]?(.*?)['""]?$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            string Parameter = null;
            string[] Parts;

            // Valid parameters forms:
            // {-,/,--}param{ ,=,:}((",')value(",'))
            // Examples: -param1 value1 --param2 /param3:"Test-:-work" /param4=happy -param5 '--=nice=--'
            foreach (string Txt in Args)
            {

                // Look for new parameters (-,/ or --) and a possible enclosed value (=,:)
                Parts = Spliter.Split(Txt, 3);
                switch (Parts.Length)
                {
                    // Found a value (for the last parameter found (space separator))
                    case 1:
                        if (Parameter != null)
                        {
                            if (!Parameters.ContainsKey(Parameter))
                            {
                                Parts[0] = Remover.Replace(Parts[0], "$1");
                                Parameters.Add(Parameter, Parts[0]);
                            }
                            Parameter = null;
                        }
                        // else Error: no parameter waiting for a value (skipped)
                        break;
                    // Found just a parameter
                    case 2:
                        // The last parameter is still waiting. With no value, set it to true.
                        if (Parameter != null)
                        {
                            if (!Parameters.ContainsKey(Parameter)) Parameters.Add(Parameter, "true");
                        }
                        Parameter = Parts[1];
                        break;
                    // Parameter with enclosed value
                    case 3:
                        // The last parameter is still waiting. With no value, set it to true.
                        if (Parameter != null)
                        {
                            if (!Parameters.ContainsKey(Parameter)) Parameters.Add(Parameter, "true");
                        }
                        Parameter = Parts[1];
                        // Remove possible enclosing characters (",')
                        if (!Parameters.ContainsKey(Parameter))
                        {
                            Parts[2] = Remover.Replace(Parts[2], "$1");
                            Parameters.Add(Parameter, Parts[2]);
                        }
                        Parameter = null;
                        break;
                }
            }
            // In case a parameter is still waiting
            if (Parameter != null)
            {
                if (!Parameters.ContainsKey(Parameter)) Parameters.Add(Parameter, "true");
            }
        }

        // Retrieve a parameter value if it exists
        public string this[string param]
        {
            get
            {
                string ret = String.Empty;

                try
                {
                    ret = Parameters[param];
                }
                catch
                {
                }

                return ret;
            }
        }
    }
}
